﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Telerik.Windows.Controls;
using Telerik.Windows.Controls.RichTextBoxUI.Dialogs;
using Telerik.Windows.Controls.RichTextBoxUI;
using Telerik.Windows.Documents.Model;
using Telerik.Windows.Documents.Layout;
using System.IO;
using Telerik.Windows.Documents.FormatProviders.Html;
using ARR.Entities;
using Telerik.Windows.Documents.FormatProviders.Pdf;

namespace ARR.UI.Views
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class RichTextEditor : Window
    {

        public GeneralComment EditingComment { get; set; }

        public RichTextEditor(GeneralComment gc)
        {
            InitializeComponent();

            EditingComment = gc;

            if (gc.Comment != null)
            {
                HtmlFormatProvider provider = new HtmlFormatProvider();
                RadDocument doc = provider.Import(gc.Comment);
                RadDocument = doc;
            }
            else
            {
                RadDocument = new RadDocument();
            }


            this.editor.ChangeSectionPageOrientation(PageOrientation.Landscape);
        }

        public RadDocument RadDocument
        {
            get
            {
                return this.editor.Document;
            }
            set
            {
                SetupNewDocument(value);
                this.editor.Document = value;
            }
        }

        private void SetupNewDocument(RadDocument document)
        {
            document.LayoutMode = DocumentLayoutMode.Paged;
            document.ParagraphDefaultSpacingAfter = 10;
            document.PageViewMargin = new SizeF(10, 10);
            document.SectionDefaultPageMargin = new Padding(95);
            document.DefaultPageLayoutSettings.Width = 793;
            document.DefaultPageLayoutSettings.Height = 1123; 
        }

        private bool IsSupportedImageFormat(string extension)
        {
            if (extension != null)
            {
                extension = extension.ToLower();
            }

            return extension == ".jpg" ||
                extension == ".jpeg" ||
                extension == ".png" ||
                extension == ".bmp";
        }

        private void Statistics_Click(object sender, RoutedEventArgs e)
        {
            string statistics = this.editor.Document.GetStatisticsInfo().ToString();
            MessageBox.Show(statistics, "Document Statistics", MessageBoxButton.OK);
        }

        private void editor_Drop(object sender, DragEventArgs e)
        {
            FileInfo[] droppedFiles = e.Data.GetData(DataFormats.FileDrop) as FileInfo[];

            foreach (FileInfo droppedFile in droppedFiles)
            {
                if (this.IsSupportedImageFormat(droppedFile.Extension))
                {
                    using (Stream imageStream = droppedFile.OpenRead())
                    {
                        this.editor.InsertImage(imageStream, droppedFile.Extension);
                    }
                }
            }
        }

        private void Save(object sender, RoutedEventArgs e)
        {
            PdfFormatProvider provider = new PdfFormatProvider();
            EditingComment.Pdf = provider.Export(RadDocument);

            HtmlFormatProvider htmlProvider = new HtmlFormatProvider();
            EditingComment.Comment = htmlProvider.Export(RadDocument);

            this.Close();
        }

    }
}
